    //

//  HomeViewController.swift
//  weibo10
//
//  Created by 晁岳山 on 15/10/14.
//  Copyright © 2015年 晁岳山. All rights reserved.
//

import UIKit

let reuseCellID = "reuseCellID"
class HomeViewController: VisiterViewController {
    
    //定义微博视图模型
    private lazy var statusList = StatusListViewModel()
    override func viewDidLoad() {
        super.viewDidLoad()
        if !UserAccountViewModel.sharedUserViewMModel.login {
            visiters?.showMessage(nil, content: nil)
            return
        }
        prepaerTable()
        loadWbData()
        //当用户点击了cell的时候  接受通知
<<<<<<< HEAD
        /// 在block通知中使用self  一定要用弱引用
        NSNotificationCenter.defaultCenter().addObserverForName(WBStatusSelectedPhotoNotification,
            object: nil,
            queue: nil) { [weak self] (notify) -> Void in
                
=======
/// 在block通知中使用self  一定要用弱引用
        NSNotificationCenter.defaultCenter().addObserverForName(WBStatusSelectedPhotoNotification,
            object: self, queue: nil) { [weak self] (notify) -> Void in
                
                print(notify)
                print(notify.userInfo)
>>>>>>> 31b3621e0c1aa1da74379c637e43582d7ba4ddfe
                guard let indexPath = notify.userInfo?["indexPath"] as? NSIndexPath else {
                    return
                }
                guard let urls = notify.userInfo?["urls"] as? [NSURL] else {
                    return
                }
<<<<<<< HEAD
=======


>>>>>>> 31b3621e0c1aa1da74379c637e43582d7ba4ddfe
                //进行控制器的跳转
                let vc = PhotoBrowserViewController(indexPath: indexPath, urls: urls)
                self?.presentViewController(vc, animated: true, completion: nil)
        }
<<<<<<< HEAD
}
=======
    }
>>>>>>> 31b3621e0c1aa1da74379c637e43582d7ba4ddfe
    
    //准备表格
    private func prepaerTable() {
        //注册cell
        tableView.registerClass(StatusCell.self, forCellReuseIdentifier: reuseCellID)
        //设置cell的高度
        tableView.estimatedRowHeight = 400
        //取消系统的默认分割线
        tableView.separatorStyle = .None
    }
    private func loadWbData() {
       statusList.loadData { (succeed) -> () in
            if !succeed {
                print("微博数据从网络加载错误")
                return
            }
//        print(self.statusList.statusList)

        //刷新数据
            self.tableView.reloadData()
        }
        
    }

}
// MARK: - 数据源方法
extension HomeViewController {
    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return statusList.statusList.count
    }
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier(reuseCellID, forIndexPath: indexPath) as! StatusCell
        cell.viewModel = statusList.statusList[indexPath.row]
        return cell
    }
    //返回每一个cell的高度
    override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        
        return statusList.statusList[indexPath.row].cellHeight
    }
}

    
    
    
    